package com.cx.base.day19_thread;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author: robert.song
 * @Date: Created in 2021/11/27
 */
@Slf4j
public class Thread_15_2_Future {
    public static void main(String[] args) {
        CountDownLatch countdownLatch = new CountDownLatch(2);
//        子线程启动，并在结束时完成计数减一
        ExecutorService service = Executors.newFixedThreadPool(4);
        //        其它三个线程执行
        Future<List> f1 = service.submit(() -> {
            try {
                log.info("begin");
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                return new ArrayList();
            }
        });

        Future<List> f2 = service.submit(() -> {
            try {
                log.info("begin");
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                return new ArrayList();
            }
        });

        Future<List> f3 = service.submit(() -> {
            try {
                log.info("begin");
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                return new ArrayList();
            }
        });
//        等待，直到其它线程结束
        service.submit(() -> {
            try {
                log.info("waitting...");
                log.info("f1.get() {}", f1.get());
                log.info("f2.get() {}", f2.get());
                log.info("f3.get() {}", f3.get());
                log.info("wait end...");
                service.shutdown();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });


    }

}
